package day5.compare;

import org.junit.Test;

import java.util.Arrays;
import java.util.Comparator;

/**
 * @author joker
 * @date 2022/4/16 0016 - 2:37
 */
public class CompareTest {
    public static void main(String[] args) {
        Goods[] goods = new Goods[6];
        goods[0] = new Goods(123, "李四");
        goods[1] = new Goods(154, "张三");
        goods[2] = new Goods(132, "王五");
        goods[3] = new Goods(45, "赵六");
        goods[4] = new Goods(353, "刘琦");
        goods[5] = new Goods(132, "黄日华");
        Arrays.sort(goods);
        System.out.println(Arrays.toString(goods));
    }
    @Test
    public void test1(){
        Goods[] goods = new Goods[6];
        goods[0] = new Goods(123, "李四");
        goods[1] = new Goods(154, "张三");
        goods[2] = new Goods(132, "王五");
        goods[3] = new Goods(45, "赵六");
        goods[4] = new Goods(353, "刘琦");
        goods[5] = new Goods(132, "黄日华");
        Arrays.sort(goods, new Comparator<Goods>() {
            @Override
            public int compare(Goods o1, Goods o2) {
                if (o1 != null && o2 != null && o1 instanceof Goods && o2 instanceof Goods) {
                    Goods o11 = o1;
                    Goods o22 = o2;
                    if(o11.getName().equals(o22.getName())){
                        return -Double.compare(o11.getPrice(),o22.getPrice());
                    }else{
                        return o11.getName().compareTo(o22.getName());
                    }
                }
                throw new RuntimeException("数据不一致");

            }
        });
        System.out.println(Arrays.toString(goods));
    }
}